<?php


namespace app\common\model;


use app\common\traites\TraitModel;
use think\Model;

class Luzhu extends Model
{
    use TraitModel;

    public $name = 'dianji_lu_zhu';
    //露珠
    public static $status = 1; //其他作废 1正常露珠
    public static $decor = ['r'=>'红桃','h'=>'黑桃','f'=>'方块','m'=>'花子'];
    public static $poker = ['1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,'10'=>10,'J'=>11,'Q'=>12,'K'=>13];
    public static function page_list($map, $limit, $page)
    {
        return self::alias('a')
            ->where($map)
            ->join('dianji_game_type b', 'a.game_type = b.id', 'left')
            ->join('dianji_table c', 'a.table_id = c.id', 'left')
            ->field('a.*,b.type_name,c.table_title')
            ->order('id desc')
            ->paginate(['list_rows' => $limit, 'page' => $page], false);
    }

    //开牌的时候插入露珠查询
    public static function open_query_one($post)
    {
        // 缺少时间s
        $nowTime = time();
        $startTime = strtotime(date("Y-m-d 04:00:00", time()));
        // 如果小于，则算前一天的
        if ($nowTime < $startTime) {
            $startTime = $startTime - (24 * 60 * 60);
        }

        return self::where([
            'status' => $post['status'],
            'table_id' => $post['table_id'],
            'xue_number' => $post['xue_number'],
            'pu_number' => $post['pu_number']
        ])
            ->where('create_time', '>', $startTime)
            ->find();
    }

    public static function open_update_one($id, $post)
    {
        return self::where('id', $id)->update([
            'status' => $post['status'],
            'game_type' => $post['game_type'],
            'result' => $post['result']
        ]);
    }

    //开牌的是会后获取靴号和铺号
    public static function open_get_xue_pu($table_id)
    {
        //获取今天最大的靴号
       $self =  self::where('table_id',$table_id)->whereTime('create_time','today')->order('id desc')->find();
        if (empty($self)){
            return ['xue_number'=>1,'pu_number'=>1];
        }
        //铺号加入才是当前铺
        return ['xue_number'=>$self->xue_number,'pu_number'=>$self->pu_number+1];
    }
}